package com.meego.common.oembed;

import com.google.gson.JsonObject;
import java.io.Serializable;

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;

import org.codehaus.jackson.map.annotate.JsonSerialize;
import org.codehaus.jackson.map.annotate.JsonSerialize.Inclusion;

@XmlRootElement(name="oembed")
@XmlAccessorType(XmlAccessType.FIELD)
@JsonSerialize(include=Inclusion.NON_NULL)
public class OembedResponse implements Serializable {
	private static final long serialVersionUID = -1965788850835022977L;

	/** This one is not mapped to json and xml but set right after parsing. The source equals the provider name */	
	@javax.xml.bind.annotation.XmlTransient
	private String source;
	/** This is the original url which got transformed */
	@javax.xml.bind.annotation.XmlTransient
	private String originalUrl;
	@javax.xml.bind.annotation.XmlTransient
	private boolean empty = false;
	
	@XmlElement(name="type")
	private String type;
	//for slideshow.com
	@XmlElement(name="slideshow_id")
	private String slideshow_id;
	@XmlElement(name="total_slides")
	private String total_slides;
	@XmlElement(name="version_no")
	private String version_no;
	@XmlElement(name="conversion_version")
	private String conversion_version;
	@XmlElement(name="thumbnail")
	private String thumbnail;
	@XmlElement(name="slide_image_baseurl_suffix")
	private String slide_image_baseurl_suffix;
	@XmlElement(name="slide_image_baseurl")
	private String slide_image_baseurl;
	//end slideshow.com
	@XmlElement(name="version")
	private String version;
	@XmlElement(name="geo_latitude")
	private String geo_latitude;
	@XmlElement(name="geo_longitude")
	private String geo_longitude;
	@XmlElement(name="gallery_title")
	private String gallery_title;
	@XmlElement(name="gallery_url")
	private String gallery_url;
	@XmlElement(name="title")
	private String title;
	@XmlElement(name="author_name")
	private String authorName;
	@XmlElement(name="images")
	private JsonObject images;
	@XmlElement(name="author_name ")
	private String authorName_;
	@XmlElement(name="author_url")
	private String authorUrl;
	@XmlElement(name="provider_name")
	private String providerName;
	@XmlElement(name="provider_url")
	private String providerUrl;
	@XmlElement(name="cache_age")
	private Long cacheAge;
	@XmlElement(name="thumbnail_url")
	private String thumbnailUrl;
	@XmlElement(name="thumbnail_width")
	private Integer thumbnailWidth;
	@XmlElement(name="thumbnail_height")
	private Integer thumbnailHeight;
	@XmlElement(name="url")
	private String url;
	@XmlElement(name="html")
	private String html;
	@XmlElement(name="width")
	private Integer width;
	@XmlElement(name="height")
	private Integer height;
	@XmlElement(name="description")
	private Integer description;

	/**
	 * @param possibleSource
	 * @return True if this source is given and equals <code>possibleSource</code>
	 */
	public boolean from(String possibleSource) {
		return this.getSource() != null && this.source.equals(possibleSource);
	}
		
	public String getSource() {
		return source;
	}

	public void setSource(String src) {
		this.source = src;
	}
	
	public String getOriginalUrl() {
		return originalUrl;
	}

	public void setOriginalUrl(String originalUrl) {
		this.originalUrl = originalUrl;
	}
	
	public String getType() {
		return type;
	}
	
	public void setType(String type) {
		this.type = type;
	}
		
	public String getVersion() {
		return version;
	}
	
	public void setVersion(String version) {
		this.version = version;
	}
		
	public String getTitle() {
		return title;
	}
	
	public void setTitle(String title) {
		this.title = title;
	}
		
	public String getAuthorName() {
		return authorName==null?authorName_:authorName;
	}
	
	public void setAuthorName(String authorName) {
		this.authorName = authorName;
	}
	
	public String getAuthorUrl() {
		return authorUrl;
	}
	
	public void setAuthorUrl(String authorUrl) {
		this.authorUrl = authorUrl;
	}
	
	public String getProviderName() {
		return providerName;
	}
	
	public void setProviderName(String providerName) {
		this.providerName = providerName;
	}
		
	public String getProviderUrl() {
		return providerUrl;
	}
	
	public void setProviderUrl(String providerUrl) {
		this.providerUrl = providerUrl;
	}
		
	public Long getCacheAge() {
		return cacheAge;
	}
	
	public void setCacheAge(Long cacheAge) {
		this.cacheAge = cacheAge;
	}
		
	public String getThumbnailUrl() {
		return thumbnailUrl == null? thumbnail:thumbnailUrl;
	}
	
	public void setThumbnailUrl(String thumbnailUrl) {
		this.thumbnailUrl = thumbnailUrl;
	}
		
	public Integer getThumbnailWidth() {
		return thumbnailWidth;
	}
	
	public void setThumbnailWidth(Integer thumbnailWidth) {
		this.thumbnailWidth = thumbnailWidth;
	}
		
	public Integer getThumbnailHeight() {
		return thumbnailHeight;
	}
	
	public void setThumbnailHeight(Integer thumbnailHeight) {
		this.thumbnailHeight = thumbnailHeight;
	}
	
	public String getUrl() {
		return url;
	}

	public void setUrl(String url) {
		this.url = url;
	}
	
	public String getHtml() {
		return html;
	}

	public void setHtml(String html) {
		this.html = html;
	}
	
	public Integer getWidth() {
		return width;
	}

	public void setWidth(Integer width) {
		this.width = width;
	}
	
	public Integer getHeight() {
		return height;
	}

	public void setHeight(Integer height) {
		this.height = height;
	}

	public boolean isEmpty() {
		return empty;
	}

	public void setEmpty(boolean empty) {
		this.empty = empty;
	}

	public void setImages(JsonObject images) {
		this.images = images;
	}

	public JsonObject getImages() {
		return images;
	}

	public void setGallery_title(String gallery_title) {
		this.gallery_title = gallery_title;
	}

	public String getGallery_title() {
		return gallery_title;
	}

	public void setGallery_url(String gallery_url) {
		this.gallery_url = gallery_url;
	}

	public String getGallery_url() {
		return gallery_url;
	}

	public void setTotal_slides(String total_slides) {
		this.total_slides = total_slides;
	}

	public String getTotal_slides() {
		return total_slides;
	}

	public void setSlideshow_id(String slideshow_id) {
		this.slideshow_id = slideshow_id;
	}

	public String getSlideshow_id() {
		return slideshow_id;
	}

	public void setSlide_image_baseurl(String slide_image_baseurl) {
		this.slide_image_baseurl = slide_image_baseurl;
	}

	public String getSlide_image_baseurl() {
		return slide_image_baseurl;
	}

	public void setSlide_image_baseurl_suffix(String slide_image_baseurl_suffix) {
		this.slide_image_baseurl_suffix = slide_image_baseurl_suffix;
	}

	public String getSlide_image_baseurl_suffix() {
		return slide_image_baseurl_suffix;
	}

	public void setGeo_latitude(String geo_latitude) {
		this.geo_latitude = geo_latitude;
	}

	public String getGeo_latitude() {
		return geo_latitude;
	}

	public void setGeo_longitude(String geo_longitude) {
		this.geo_longitude = geo_longitude;
	}

	public String getGeo_longitude() {
		return geo_longitude;
	}

	public void setDescription(Integer description) {
		this.description = description;
	}

	public Integer getDescription() {
		return description;
	}	

	@Override
	public String toString() {
		return "OembedResponse [type=" + type + ", version=" + version
				+ ", title=" + title + ", authorName=" + authorName
				+ ", authorUrl=" + authorUrl + ", providerName=" + providerName
				+ ", providerUrl=" + providerUrl + ", cacheAge=" + cacheAge
				+ ", thumbnailUrl=" + thumbnailUrl + ", thumbnailWidth="
				+ thumbnailWidth + ", thumbnailHeight=" + thumbnailHeight
				+ ", url=" + url + ", html=" + html + ", width=" + width
				+ ", height=" + height + ", images=" + images + ", description=" + description
				+ ", geo_latitude=" + geo_latitude + ", geo_longitude=" + geo_longitude
				+ ", total_slides=" + total_slides + ", slideshow_id=" + slideshow_id
				+ ", slide_image_baseurl=" + slide_image_baseurl + ", slide_image_baseurl_suffix=" + slide_image_baseurl_suffix
				+ ", gallery_title=" + gallery_title + ", gallery_url=" + gallery_url + "]";
	}
	
	/**
	 * Renders a html representation of this oembed response
	 * @return
	 */
	public String render() {
		String rv = null;
		if(this.getType().equalsIgnoreCase("photo"))
			rv = this.renderPhoto();
		else if(this.getType().equalsIgnoreCase("video"))
			rv = this.renderVideo();
		else if(this.getType().equalsIgnoreCase("link"))
			rv = this.renderLink();
		else if(this.getType().equalsIgnoreCase("rich"))
			rv = this.renderRich();
		return rv;
	}
	
	private String renderVideo() {
		return this.getHtml();
	}
	
	private String renderPhoto() {
		final String _title = this.getTitle() == null ? "" : this.getTitle();
		return String.format("<img src=\"%s\" style=\"width:%dpx; height:%dpx;\" alt=\"%s\" title=\"%s\"/>", this.getUrl(), this.getWidth(), this.getHeight(), _title, _title);
	}
	
	private String renderLink() {
		final String _title = this.getTitle() == null ? this.getOriginalUrl() : this.getTitle();
		final String _url = this.getUrl() == null ? this.getOriginalUrl() : this.getUrl();
		return String.format("<a href=\"%s\">%s</a>", _url, _title);
	}
	
	private String getTargetUrl(){
		if(this.getType().equalsIgnoreCase("photo")){
			return this.getUrl();
		}
		else if(this.getType().equalsIgnoreCase("link")){
			return this.getUrl() == null ? this.getOriginalUrl() : this.getUrl();
		}
		return null;
	}
	
	private String renderRich() {
		return this.getHtml();
	}
	public JsonObject toJSON(){
		JsonObject json = new JsonObject();
		json.addProperty("source", getSource());
		json.addProperty("originalUrl", getOriginalUrl());
		json.addProperty("type", getType());
		json.addProperty("version", getVersion());
		json.addProperty("thumbnail", getThumbnailUrl());
		json.addProperty("thumbnailWidth", getThumbnailWidth());
		json.addProperty("thumbnailHeight", getThumbnailHeight());
		json.addProperty("gallery_title", getGallery_title());
		json.addProperty("gallery_url", getGallery_url());
		json.add("images", getImages());
		json.addProperty("title", getTitle());
		json.addProperty("authorName", getAuthorName());
		json.addProperty("authorUrl", getAuthorUrl());
		json.addProperty("providerName", getProviderName());
		json.addProperty("providerUrl", getProviderUrl());
		json.addProperty("description", getDescription());
		json.addProperty("embed", render());//ma nhung
		json.addProperty("target_url", getTargetUrl());//dc dung trong truong hop type=photo hoac link ma client khong dung embed(<img ..../> hoac <a herf ....>)
		json.addProperty("width", getWidth());
		json.addProperty("height", getHeight());
		return json;
	}
	public String marshal() throws OembedException {
		OembedJsonParser p = new OembedJsonParser();
		return p.marshal(this);
	}
}